var express = require('express');
const {json} = require('express/lib/response');
var router = express.Router();
var db = require('../model/index')
// const date = require('../public/package')

/* 1-1促销管理，2抵扣管理 */
router.get('/promotion/getallcxgl', function (req, res, next) {
  let mysql = `SELECT * FROM promotion_cxgl`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '查询失败，请稍后重试'
      })
    }
        /* 筛选 */
        if (JSON.stringify(req.query) != '{}') {
          for (let i = 0; i < data.length; i++) {
            if (data[i].cxbt.indexOf(req.query.cxbt) == -1) {
              data.splice(i, 1);
              i--;
            } else if (req.query.sfsj != '全部') {
              if (data[i].sfsj != req.query.sfsj) {
                data.splice(i, 1);
                i--;
              };
            } else if (data[i].cxsp.indexOf(req.query.cxsp) == -1) {
              data.splice(i, 1);
              i--;
            } else if (req.query.syxc != '全部') {
              if (data[i].syxc != req.query.syxc) {
                data.splice(i, 1);
                i--;
              };
            }
          }
        };
    res.json({
      code: 200,
      msg: '查询成功',
      data: data
    })
  })
});
/* 1-2商品管理 */
router.get('/promotion/getallspgl', function (req, res, next) {
  let mysql = `SELECT * FROM promotion_spgl`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '查询失败，请稍后重试'
      })
    }
    res.json({
      code: 200,
      msg: '查询成功',
      data: data
    })
  })
});
/* 3抵扣管理 */
router.get('/promotion/getalldkgl', function (req, res, next) {
  let mysql = `SELECT * FROM promotion_dkgl`;
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '查询失败，请稍后重试'
      })
    }
    /* 筛选 */
    if (JSON.stringify(req.query) != '{}') {
      for (let i = 0; i < data.length; i++) {
        // let tjsj = data[i].tjsj.split('~');
        // let startTime = Date.parse(tjsj[0].replace('-', '/'));
        // let endTime = Date.parse(tjsj[1].replace('-', '/'));
        if (data[i].dkbt.indexOf(req.query.dkbt) == -1) {
          data.splice(i, 1);
          i--;
        } else if (req.query.sfsj != '全部') {
          if (data[i].sfsj != req.query.sfsj) {
            data.splice(i, 1);
            i--;
          };
        } else if (req.query.syxc != '全部') {
          if (data[i].syxc != req.query.syxc) {
            data.splice(i, 1);
            i--;
          };
        } 
        // else if (req.query.tjsj) {
        //   let time = Date.parse(req.query.tjsj.replace('-', '/'));
        //   if (time == startTime || time > endTime) {
        //     data.splice(i, 1);
        //     i--;
        //   }
        // }
      }
    };
    res.json({
      code: 200,
      msg: '查询成功',
      data: data
    })
  })
});

/*3抵扣管理是否上架滑动开关 */
router.post('/promotion/dkglsfsj', function (req, res, next) {
  let mysql = `UPDATE promotion_dkgl SET sfsj='${req.body.sfsj}' 
  WHERE id='${req.body.id}';`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '上架失败,请稍后重试',
      })
    }
    res.json({
      code: 200,
      msg: '上架成功',
      data: data
    })
  })
});
/*2抢购管理是否上架滑动开关，1-1促销管理是否上架滑动开关 */
router.post('/promotion/cxglsfsj', function (req, res, next) {
  let mysql = `UPDATE promotion_cxgl SET sfsj='${req.body.sfsj}' 
  WHERE id='${req.body.id}';`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '上架失败,请稍后重试',
      })
    }
    res.json({
      code: 200,
      msg: '上架成功',
      data: data
    })
  })
});
/*1-2商品管理是否上架滑动开关 */
router.post('/promotion/spglsfsj', function (req, res, next) {
  let mysql = `UPDATE promotion_spgl SET sfsj='${req.body.sfsj}' 
  WHERE id='${req.body.id}';`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '上架失败,请稍后重试',
      })
    }
    res.json({
      code: 200,
      msg: '上架成功',
      data: data
    })
  })
});

/* 3抵扣管理数据删除 */
router.post('/promotion/dkglsc',function(req,res,next){
  let mysql=`DELETE FROM promotion_dkgl 
  WHERE id='${req.body.id}';`
  db(mysql,function(err,data){
    if(err){
      return res.json({
        code:500,
        msg:'删除失败，请稍后重试'
      })
    }
    res.json({
      code:200,
      msg:'删除成功',
      data:data
    })
  })
});
/* 2抢购管理与1-1促销管理数据删除 */
router.post('/promotion/cxglsc',function(req,res,next){
  let mysql=`DELETE FROM promotion_cxgl 
  WHERE id='${req.body.id}';`
  db(mysql,function(err,data){
    if(err){
      return res.json({
        code:500,
        msg:'删除失败，请稍后重试'
      })
    }
    res.json({
      code:200,
      msg:'删除成功',
      data:data
    })
  })
});
/* 1-2商品管理数据删除 */
router.post('/promotion/spglsc',function(req,res,next){
  let mysql=`DELETE FROM promotion_spgl 
  WHERE id='${req.body.id}';`
  db(mysql,function(err,data){
    if(err){
      return res.json({
        code:500,
        msg:'删除失败，请稍后重试'
      })
    }
    res.json({
      code:200,
      msg:'删除成功',
      data:data
    })
  })
});

/* 3抵扣管理添加数据 */
router.post('/promotion/dkgltj',function(req,res,next){
  let mysql = `INSERT INTO promotion_dkgl VALUES 
  (NULL,'${req.body.dkbt}','${req.body.dkbz}',
  '${req.body.syxc}','${req.body.sfsj}',CURRENT_TIMESTAMP);`;
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '添加失败,请稍后重试',
        data:err
      })
    }
    res.json({
      code: 200,
      msg: '添加成功',
      data: data
    })
  })
});
/* 2抢购管理与1-1促销管理添加数据 */
router.post('/promotion/cxgltj',function(req,res,next){
  let mysql = `INSERT INTO promotion_cxgl VALUES 
  (NULL,'${req.body.cxbt}','${req.body.cxxq}',
  '${req.body.syxc}','${req.body.sfsj}',CURRENT_TIMESTAMP,
  '${req.body.cxsp}','${req.body.cxhh}','${req.body.kssj}');`;
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '添加失败,请稍后重试',
        data:err
      })
    }
    res.json({
      code: 200,
      msg: '添加成功',
      data: data
    })
  })
});
/*  1-2商品管理添加数据 */
router.post('/promotion/spgltj',function(req,res,next){
  let mysql = `INSERT INTO promotion_spgl VALUES 
  (NULL,'','${req.body.spmc}',
  '${req.body.hh}','${req.body.jg}','${req.body.fl}',
  '${req.body.xl}','${req.body.wz}','${req.body.sfsj}');`;
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '添加失败,请稍后重试',
        data:err
      })
    }
    res.json({
      code: 200,
      msg: '添加成功',
      data: data
    })
  })
});

/* 3抵扣管理编辑数据 */
router.post('/promotion/dkglbj', function (req, res, next) {
  if (req.body.sfsj) {
    req.body.sfsj = '是'
  } else {
    req.body.sfsj = '否'
  }
  let mysql = `UPDATE promotion_dkgl SET 
  dkbt='${req.body.dkbt}',dkbz='${req.body.dkbz}',
  syxc='${req.body.syxc}',sfsj='${req.body.sfsj}'
  WHERE id='${req.body.id}';`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '编辑失败，请稍后重试',
        data: err
      })
    }
    res.json({
      code: 200,
      msg: '编辑成功',
      data: data
    })
  })
});
/* 2抢购管理与1-1促销管理编辑数据 */
router.post('/promotion/cxglbj', function (req, res, next) {
  if (req.body.sfsj) {
    req.body.sfsj = '是'
  } else {
    req.body.sfsj = '否'
  }
  let mysql = `UPDATE promotion_cxgl SET 
  cxbt='${req.body.cxbt}',cxxq='${req.body.cxxq}',
  syxc='${req.body.syxc}',sfsj='${req.body.sfsj}',
  cxsp='${req.body.cxsp}',cxhh='${req.body.cxhh}',
  kssj='${req.body.kssj}'
  WHERE id='${req.body.id}';`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '编辑失败，请稍后重试',
        data: err
      })
    }
    res.json({
      code: 200,
      msg: '编辑成功',
      data: data
    })
  })
});
/* 1-2商品管理编辑数据 */
router.post('/promotion/spglbj', function (req, res, next) {
  if (req.body.sfsj) {
    req.body.sfsj = '是'
  } else {
    req.body.sfsj = '否'
  }
  let mysql = `UPDATE promotion_spgl SET 
  spmc='${req.body.spmc}',hh='${req.body.hh}',
  jg='${req.body.jg}',fl='${req.body.fl}',
  xl='${req.body.xl}',wz='${req.body.wz}',
  sfsj='${req.body.sfsj}'
  WHERE id='${req.body.id}';`
  db(mysql, function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '编辑失败，请稍后重试',
        data: err
      })
    }
    res.json({
      code: 200,
      msg: '编辑成功',
      data: data
    })
  })
});



module.exports = router;
// 1-2商品管理，为第一网页的第二表单，使用 promotion_spgl
// 1-1促销管理，为第一网页的第一表单，与2使用同样数据表 promotion_cxgl
// 2抵扣管理，为第二网页表单，与1-1使用同样数据表 promotion_cxgl
// 3抵扣管理，为第三网页表单，使用 promotion_dkgl